﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using CodeGenLib;
using EnvDTE;
using EnvDTE80;

namespace CodeGen
{
    public partial class Main : Form
    {
        public Main(DTE2 applicationObject, AddIn addInInstance)
        {
            InitializeComponent();

            _applicationObject = applicationObject;
            _addInInstance = addInInstance;
            this.seletedItem = _applicationObject.SelectedItems.Item(1);
        }

        private void Main_Load(object sender, EventArgs e)
        {
            this.controller = new Controller();

            this.ClientSize = new Size(640, 480);

            this.collectionPanel = new ParamCollectionPanel(this.controller);
            this.collectionPanel.Location = new Point(0, 0);
            this.collectionPanel.ClientSize = new Size(640, 80);
            this.collectionPanel.Show();
            this.Controls.Add(this.collectionPanel);
            this.controller.CollectView = this.collectionPanel;

            this.hPanel = new PreviewPanel();
            this.hPanel.Location = new Point(0, 80);
            this.hPanel.ClientSize = new Size(320, 370);
            this.hPanel.Show();
            this.Controls.Add(this.hPanel);
            this.controller.HView = this.hPanel;

            this.cppPanel = new PreviewPanel();
            this.cppPanel.Location = new Point(320, 80);
            this.cppPanel.ClientSize = new Size(320, 370);
            this.cppPanel.Show();
            this.Controls.Add(this.cppPanel);
            this.controller.CppView = this.cppPanel;

            this.acceptBtn.Top = 454;
            this.cancelBtn.Top = 454;
            //this.acceptBtn.Top = this.Bottom-this.acceptBtn.Height-3;
            //this.cancelBtn.Top = this.Bottom-this.cancelBtn.Height-3;

            this.CenterToScreen();
        }

        private Controller controller;

        private ParamCollectionPanel collectionPanel;
        private PreviewPanel hPanel;
        private PreviewPanel cppPanel;

        private DTE2 _applicationObject;
        private AddIn _addInInstance;

        private SelectedItem seletedItem;

        private void acceptBtn_Click(object sender, EventArgs e)
        {
            String path = this.GetPath();

            bool result = this.CreateHFile(path);
            if (!result)
            {
                return;
            }

            result = this.CreateCppFile(path);
            if (!result)
            {
                return;
            }

            this.Close();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private String GetPath()
        {
            String path = this.GetProjectPath();
            if (String.IsNullOrEmpty(path))
            {
                return "";
            }

            int index = path.LastIndexOf("\\");
            if (-1 == index)
            {
                index = path.LastIndexOf("/");
            }
            if (-1 == index)
            {
                return "";
            }

            return path.Substring(0, index + 1);
        }

        private String GetProjectPath()
        {
            SelectedItem item = _applicationObject.SelectedItems.Item(1);

            if (item.Project != null)
            {
                return item.Project.FileName;
            }

            if (item.ProjectItem != null)
            {
                return item.ProjectItem.ContainingProject.FileName;
            }

            return "";
        }

        private bool CreateHFile(String path)
        {
            String fullpath = path + this.controller.Param.HFilename + ".h";
            return this.CreateFile(fullpath, this.controller.HCodes);
        }

        private bool CreateCppFile(String path)
        {
            String fullpath = path + this.controller.Param.CppFilename+ ".cpp";
            return this.CreateFile(fullpath, this.controller.CppCodes);
        }

        private bool CreateFile(String fullpath, Codes codes)
        {
            if (File.Exists(fullpath))
            {
                MessageBox.Show("file exists: " + fullpath);
                return false;
            }

            using (StreamWriter sw = File.CreateText(fullpath))
            {
                foreach (String line in codes.Lines)
                {
                    sw.WriteLine(line);
                }
            }

            this.AddToProject(fullpath);

            return true;
        }

        private void AddToProject(String filepath)
        {
            ProjectItems items = this.GetProjectItems();
            if (items == null)
            {
                return;
            }

            items.AddFromFile(filepath);
            _applicationObject.OpenFile(Constants.vsViewKindPrimary, filepath);
      }

        private ProjectItems GetProjectItems()
        {
            if (this.seletedItem.Project != null)
            {
                return this.seletedItem.Project.ProjectItems;
            }

            if (this.seletedItem.ProjectItem != null)
            {
                return this.seletedItem.ProjectItem.ProjectItems;
            }

            return null;
        }
    }
}
